package com.zretc.controller;


import com.zretc.service.ChooseSeatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/getSeat")
public class ChooseSeatController {
    private static  final Logger logger = LoggerFactory.getLogger(ChooseSeatController.class);

    @Autowired
    private ChooseSeatService chooseSeatService;

    @RequestMapping("/findSeat")
    public @ResponseBody
    List<Map<String, Object>> getBroadCastSeat(@RequestParam(value = "hallId") String hallId,
                                               @RequestParam(value = "cinemaId") String cinemaId,
                                               @RequestParam(value = "movieId") String movieId,
                                               @RequestParam(value = "startTime") String startTime


    ){
        Map<String,Object> param = new HashMap<>();
        param.put("hallId",hallId);
        param.put("cinemaId",cinemaId);
        param.put("movieId",movieId);
        param.put("startTime",startTime);
        List<Map<String, Object>> broadCastHallSeatByHallId = chooseSeatService.getBroadCastHallSeatByHallId(param);
        return broadCastHallSeatByHallId;


    }
    @RequestMapping("/addOrder")
    @ResponseBody
    public boolean addMoviceOrder(
                                            @RequestParam("seatId[]") List<String> seatId,
                                            @RequestParam(value = "price") Integer price,
                                            @RequestParam(value = "movieId") String movieId,
                                            @RequestParam(value = "userId") String userId,
                                            @RequestParam(value = "cinemaId") String cinemaId,

                                            @RequestParam(value = "hallId") String hallId,

                                            HttpServletRequest request
    ) {
        Map<String, Object> param = new HashMap<>();

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddhhmmss");
        String format = ofPattern.format(now);
        int v = (int) (Math.random() * 1000 + 1);
        StringBuffer n = new StringBuffer();

        n.append(format).append(v);

        String ordersId = n.toString();

        HttpSession httpSession = request.getSession();
        httpSession.setAttribute("orderId",ordersId);

        logger.info(ordersId.toString()+"===============");




        param.put("orderId", ordersId);
        param.put("movieId", movieId);
        param.put("userId", userId);
        param.put("cinemaId", cinemaId);
        param.put("hallId", hallId);

        param.put("seatIdlist",seatId);
        param.put("price",price);


        boolean i = chooseSeatService.addPayOrderInfo(param);
        return i;



        /*HttpSession session = request.getSession();
        */



    }

    @RequestMapping("/addSeat")
    public boolean addMoviceSeat(

            @RequestParam(value = "hallId") String hallId,
            @RequestParam(value = "xAxis") Integer xAxis,
            @RequestParam(value = "yAxis") String yAxis

    ){
        Map<String,Object> param = new HashMap<>();
        param.put("hallId",hallId);
        param.put("xAxis",xAxis);
        param.put("yAxis",yAxis);
        boolean i = chooseSeatService.addSeatInfo(param);

        /*ModelAndView modelAndView = new ModelAndView();*/


           /* modelAndView.setViewName("redirect:/front/payindex.jsp");*/



        return i;


    }

    @RequestMapping("/updateOrder")
    public  void updateOrder(String orderId){

        boolean b = chooseSeatService.updateOrderStatus(orderId);

        ModelAndView modelAndView = new ModelAndView();

        if (b==true){
            modelAndView.setViewName("redirect:/front/choose_seat.jsp");
        }



    }
}
